package com.gzhryc.shared_device.oem.code.member.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.member.dao.MemberAgreementDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberAgreement;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberAgreementEnum;

public class MemberAgreementService {

	static Logger log = Logger.getLogger(MemberAgreementService.class);

    public static MemberAgreementService self() {
        return MultiDBTools.getService(MemberAgreementService.class, true);
    }

    public static MemberAgreementService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, MemberAgreementService.class, true);
    }

    private String jdbcKey;

    public MemberAgreementService(String jdbcKey) {
        this.jdbcKey = jdbcKey;
    }
    
    public List<MemberAgreement> findByMemberId(Long memberId){
    	if(memberId != null) {
    		MemberAgreementDao dao = new MemberAgreementDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberAgreement.class);
            conditions.and().is("memberId",memberId);
            
            try {
				return dao.findByCondition(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public MemberAgreement getByKeys(Long memberId, String agreementCode,String agreementAppId) {
        if (memberId != null && StringTools.isNotBlank(agreementCode) && StringTools.isNotBlank(agreementAppId)) {
        	MemberAgreementDao dao = new MemberAgreementDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberAgreement.class);
            conditions.and().is("memberId",memberId);
            conditions.and().is("agreementCode",agreementCode);
            conditions.and().is("agreementAppId",agreementAppId);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    public boolean signing(Long memberId, String agreementCode,String agreementAppId) {
    	if (memberId != null && StringTools.isNotBlank(agreementCode) && StringTools.isNotBlank(agreementAppId)) {
    		MemberAgreement memberAgreement = this.getByKeys(memberId, agreementCode, agreementAppId);
    		if(memberAgreement != null) {
    			if(MemberAgreementEnum.State.Signing.index().equals(memberAgreement.getState())) {
    				return true;
    			}
	        	MemberAgreementDao dao = new MemberAgreementDao(jdbcKey,memberId);
	
	            Conditions conditions = new Conditions(MemberAgreement.class);
	            conditions.and().is("memberId",memberId);
	            conditions.and().is("agreementCode",agreementCode);
	            conditions.and().is("agreementAppId",agreementAppId);
	            
	            MemberAgreement updateEntity = new MemberAgreement();
	            updateEntity.setState(MemberAgreementEnum.State.Signing.index());
	            updateEntity.setRefuseDate(new Date());
	
	            try {
	                return dao.updateNotNull(updateEntity,conditions) > 0;
	            } catch (SQLException e) {
	                log.error(e.getMessage(), e);
	            }
    		}else {
    			memberAgreement = new MemberAgreement();
    			memberAgreement.setMemberId(memberId);
    			memberAgreement.setAgreementCode(agreementCode);
    			memberAgreement.setAgreementAppId(agreementAppId);
    			memberAgreement.setState(MemberAgreementEnum.State.Signing.index());
    			memberAgreement.setSigningDate(new Date());
    			memberAgreement.setCreateDate(new Date());
    			
    	    	MemberAgreementDao dao = new MemberAgreementDao(jdbcKey,memberAgreement.getMemberId());
    	    	try {
    	            return dao.insert(memberAgreement) > 0;
    	        } catch (SQLException e) {
    	            log.error(e.getMessage(), e);
    	        }
    		}
        }
    	return false;
    }
    
    public boolean refuse(Long memberId, String agreementCode,String agreementAppId) {
    	if (memberId != null && StringTools.isNotBlank(agreementCode) && StringTools.isNotBlank(agreementAppId)) {
        	MemberAgreementDao dao = new MemberAgreementDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberAgreement.class);
            conditions.and().is("memberId",memberId);
            conditions.and().is("agreementCode",agreementCode);
            conditions.and().is("agreementAppId",agreementAppId);
            
            MemberAgreement updateEntity = new MemberAgreement();
            updateEntity.setState(MemberAgreementEnum.State.Refuse.index());
            updateEntity.setRefuseDate(new Date());

            try {
                return dao.updateNotNull(updateEntity,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
    	return false;
    }
}
